package com.company;

import com.company.creature.*;
import com.company.handler.*;
import com.company.room.Room;
import com.company.room.RoomSet;
import com.company.shop.*;
import com.company.skill.*;


import java.io.*;
import java.util.*;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/4/26 13:16
 * @version: v
 * @modified By:
 */
public class Scene {


    //变量定义


    private ShopSet shopSet=new ShopSet();
    private RoomSet roomSet=new RoomSet();
    private Person person =new Person();
    private MonsterSet monsterSet=new MonsterSet();
    private Ui ui=new Ui();
    private HashMap<String,Handler> handlers=new HashMap<>();
    private PskillSet pskillSet=new PskillSet();
    private MskillSet mskillSet=new MskillSet();

    //所有初始化的东西记得放进去！！顺道加get set
    public void initScene(String name){
        /**
         * @descript :初始化场景
         * @author :ZhaoYicong
         * @date :2020/4/28 15:28
         * @Param: name
         * @return :void
         * @throws :
         * @since :
         */
        initShopSet();
        initRoom();
        initHandler();
        initPskillSet();
        initMskillSet();
        initPerson(name);
        initMonster();

    }



    //各个东西的初始化
    public void initHandler(){
        /**
         * @descript :设置系统指令
         * @author :ZhaoYicong
         * @date :2020/4/28 15:27
         * @Param:
         * @return :void
         * @throws :
         * @since :
         */

        handlers.put("chop",new HandlerChop(this));
        handlers.put("help",new HandlerHelp(this));
        handlers.put("bye",new HandlerBye(this));
        handlers.put("go",new HandlerGo(this));
        handlers.put("shop",new HandlerShop(this));
        handlers.put("buy",new HandlerBuy(this));
        handlers.put("show",new HanderShow(this));
        handlers.put("use",new HanderUse(this));
        handlers.put("new",new HandlerNew(this));


    }
    public void initPerson(String name){
        /**
         * @descript :初始化人物
         * @author :ZhaoYicong
         * @date :2020/4/28 15:28
         * @Param: name
         * @return :void
         * @throws :
         * @since :
         */
    //  设置属性

        BufferedReader bf = null;
        try {
            bf = new BufferedReader(new FileReader("./初始化/人物初始化.txt"));

            String s=bf.readLine();
            while ((s=bf.readLine())!=null){
                String[] split = s.split("\t");
                if (!split[0].equals("")){
                    person.setId(split[0]);
                    person.setHp(Integer.parseInt(split[1]));
                    person.setMoney(Integer.parseInt(split[2]));
                    person.setHplimit(Integer.parseInt(split[3]));
                    person.setMp(Integer.parseInt(split[4]));
                    person.setIniHurt(Integer.parseInt(split[5]));
                    person.setFloor_flag(Integer.parseInt(split[6]));
                    person.setCurrentWeapon((Weapon) shopSet.searchById(split[7]));
                    person.setNowRoom((Room) getRoomSet().searchById(split[8]).get(Integer.parseInt(split[9])-1));
                    person.setArmor((Armor) shopSet.searchById(split[10]));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bf!=null){

                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


//        person.setId("201902227");
        person.setName(name);
//        person.setHp(200);
//        person.setMoney(1500);
//        person.setHplimit(200);
//        person.setIniHurt(-20);
//        person.setMp(500);
//        person.setFloor_flag(1);
//        person.setCurrentWeapon(new WeaponKnif("pw1","短刀",-25));
//
//        person.setCurrentWeapon((Weapon) getShopSet().searchById("w2"));

        //匿名类
//        person.setCurrentWeapon(new Weapon(0,"pw1","小刀",-5) {
//
//
//
//            @Override
//            public void useWeapon(Creature creature, Creature targetCreature) {
//                targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
//            }
//
//            @Override
//            public void function(Creature creature) {
//
//            }
//        });
        //出生地方
//        person.setNowRoom((Room) roomSet.searchById("1").get(0));

    }
    public void initMonster(){

        //初始化  小怪物  0


        BufferedReader fr = null;

        try {
            fr = new BufferedReader(new FileReader(new File("./初始化/怪兽初始化.txt")));

            String string;
            int i = 0;
            string=fr.readLine();
            while ((string=fr.readLine())!=null){
                String[] split = string.split("\t");
                   if (!split[0].equals("")){


                       monsterSet.add(new Monster(split[0],split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),Integer.parseInt(split[4])));

                       monsterSet.getMonsters().get(i).setIniHurt(Integer.parseInt(split[5]));

                       monsterSet.getMonsters().get(i).setNowRoom((Room) roomSet.searchById(split[6]).get(Integer.parseInt(split[7])-1));
                       monsterSet.getMonsters().get(i).setCurrentWeapon(new Weapon(Integer.parseInt(split[8]),split[9],split[10],Integer.parseInt(split[11])) {
                           @Override
                           public void useWeapon(Creature creature, Creature targetCreature) {
                               targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
                           }

                           @Override
                           public void function(Creature creature) {

                           }
                       });
                       i++;


                   }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }




//        Monster monster1=new Monster("m1","窃贼1",50,50,0);
//        Monster monster2 = new Monster("m2","窃贼2",50,50,0);
//        Monster monster3 = new Monster("m3","窃贼3",50,50,0);




        //boss 1
//        Monster monster=new Monster("b1","远古巨龙",(int) (Math.random()*1000+1000),0,1);


        //初始武器！
//        monster1.setCurrentWeapon(new Weapon(0,"pw1","屠龙刀1",-5) {
//            @Override
//            public void useWeapon(Creature creature, Creature targetCreature) {
//                targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
//            }
//
//            @Override
//            public void function(Creature creature) {
//
//            }
//        });


//        monster2.setCurrentWeapon(new Weapon(0,"pw1","屠龙刀2",-5) {
//            @Override
//            public void useWeapon(Creature creature, Creature targetCreature) {
//                targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
//            }
//
//            @Override
//            public void function(Creature creature) {
//
//            }
//        });


//        monster3.setCurrentWeapon(new Weapon(0,"pw1","屠龙刀3",-5) {
//            @Override
//            public void useWeapon(Creature creature, Creature targetCreature) {
//                targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
//            }
//
//            @Override
//            public void function(Creature creature) {
//
//            }
//        });


//        monster.setCurrentWeapon(new Weapon(0,"mw4","黄金匕首",-(int) (Math.random()*100+200)) {
//            @Override
//            public void useWeapon(Creature creature, Creature targetCreature) {
//                targetCreature.setHp(targetCreature.getHp()+this.getHurtValue()+creature.getIniHurt());
//            }
//
//            @Override
//            public void function(Creature creature) {
//
//            }
//        });


        //设置基础伤害

//        monster.setIniHurt(-200);


        //出生的房间
//        monster.setNowRoom(rRoom(5,5));


//        monsterSet.add(monster);




    }
    public void initShopSet(){
        /**
         * @descript : 初始化商店的各个东西
         * @author :ZhaoYicong
         * @date :2020/5/13 22:08
         * @Param:
         * @return :void
         * @throws :
         * @since :
         */






        HpPotion hpPotion = new HpPotion(10,"s1","生命药水");
        MpPotion mpPotion=new MpPotion(10,"s2","魔法药水");
//        RandomAttack randomAttack=new RandomAttack(50,"s3","重置攻击力");
//        RandomBlood randomBlood=new RandomBlood(50,"s4","重置血量上限");
//        WeaponShop weaponShop1 = new WeaponShop();
//        WeaponShop weaponShop2=new WeaponShop();

//        ArmorShop armorShop1=new ArmorShop(600,"a1","魔铠");
//
//        armorShop1.setDefenseValue(500);
        BufferedReader bra= null;
        //初始化防具

        try {
            bra = new BufferedReader(new FileReader("./初始化/商店初始化防具.txt"));

            String s= bra.readLine();
            while ((s=bra.readLine())!=null){
                String[] split = s.split("\t");
                if (!split[0].equals("")){
                    shopSet.add(new ArmorShop(Integer.parseInt(split[0]),split[1],split[2],Integer.parseInt(split[3])));

                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bra!=null){

                try {
                    bra.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        //初始化武器

        BufferedReader brw= null;

        try {
            brw = new BufferedReader(new FileReader("./初始化/商店初始化武器.txt"));

            String s= brw.readLine();
            while ((s=brw.readLine())!=null){
                String[] split = s.split("\t");
                if (!split[0].equals("")){
                    shopSet.add(new WeaponShop(Integer.parseInt(split[0]),split[1],split[2],Integer.parseInt(split[3])));

                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(brw!=null){

                try {
                    brw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


//        weaponShop1.setSmoney(200);
//        weaponShop1.setId("w1");
//        weaponShop1.setName("破军");
//        weaponShop1.setHurtValue(-500);
//
//        weaponShop2.setId("w2");
//        weaponShop2.setSmoney(250);
//        weaponShop2.setName("逐日之弓");
//        weaponShop2.setHurtValue(-600);





        shopSet.add(hpPotion);
        shopSet.add(mpPotion);
//        shopSet.add(randomAttack);
//        shopSet.add(randomBlood);
//        shopSet.add(weaponShop1);
//        shopSet.add(weaponShop2);
//        shopSet.add(armorShop1);


    }
    public void initRoom(){
        /**
         * @descript : 初始化房间
         * @author :ZhaoYicong
         * @date :2020/4/27 22:01
         * @Param:
         * @return :void
         * @throws :
         * @since :
         */

        //创建房间和名字
        ArrayList<Room> rooms = new ArrayList<>();
        ArrayList<ArrayList> arrayLists = new ArrayList<>();
        ArrayList<Room> temp=new ArrayList<>();
        Object clone=null;


        int flag=0;
        //帮助用户可以在创建时 随意按空格
        int flag_c=1;

        BufferedReader bf= null;
        try {
            bf = new BufferedReader(new FileReader("./初始化/房间初始化.txt"));
            String s;
            s=bf.readLine();
            while ((s=bf.readLine())!=null){
                String[] split = s.split("\t");
                if (!split[0].equals("")){
                    rooms.add(new Room(split[0],split[1],split[2]));
                    flag++;
                }



                if (flag%5==0 && flag/5==flag_c){
                    flag_c++;

                     //所有1号房间的设置。
                    rooms.get((flag/5-1)*5).setNextRoom("2", rooms.get((flag/5-1)*5+1));
                    rooms.get((flag/5-1)*5).setNextRoom("4", rooms.get((flag/5-1)*5+3));
                    rooms.get((flag/5-1)*5).setNextRoom("3", rooms.get((flag/5-1)*5+2));
                    //所有2号房间的设置
                    rooms.get((flag/5-1)*5+1).setNextRoom("1",rooms.get((flag/5-1)*5));
                    rooms.get((flag/5-1)*5+1).setNextRoom("3",rooms.get((flag/5-1)*5+2));
                    rooms.get((flag/5-1)*5+1).setNextRoom("5",rooms.get((flag/5-1)*5+4));
                    //所有3号房间的设置
                    rooms.get((flag/5-1)*5+2).setNextRoom("2",rooms.get((flag/5-1)*5+1));
                    rooms.get((flag/5-1)*5+2).setNextRoom("4",rooms.get((flag/5-1)*5+3));
                    rooms.get((flag/5-1)*5+2).setNextRoom("1",rooms.get((flag/5-1)*5));
                    rooms.get((flag/5-1)*5+2).setNextRoom("5",rooms.get((flag/5-1)*5+4));
                    //所有4号房间的设置
                    rooms.get((flag/5-1)*5+3).setNextRoom("3",rooms.get((flag/5-1)*5+2));
                    rooms.get((flag/5-1)*5+3).setNextRoom("1",rooms.get((flag/5-1)*5));
                    rooms.get((flag/5-1)*5+3).setNextRoom("5",rooms.get((flag/5-1)*5+4));
                    //所有5号房间的设置
                    rooms.get((flag/5-1)*5+4).setNextRoom("2",rooms.get((flag/5-1)*5+1));
                    rooms.get((flag/5-1)*5+4).setNextRoom("3",rooms.get((flag/5-1)*5+2));
                    rooms.get((flag/5-1)*5+4).setNextRoom("4",rooms.get((flag/5-1)*5+3));

                    //复制相关楼层的数据。
                    for (int i = (flag/5-1)*5; i < flag; i++) {
                        temp.add(rooms.get(i));
                    }
                    clone = temp.clone();
                    arrayLists.add((ArrayList) clone);

                    roomSet.getRoomHashMap().put(split[0],arrayLists.get(flag/5-1));
                    temp.clear();


                }
            }
            int floor=flag/5;
            //最后设置上楼的情况
            for (int i=0;i<floor;i++){
                if (i-1>=0){

                    rooms.get(i*5).setNextRoom("down",rooms.get((i-1)*5));
                    rooms.get(i*5+4).setNextRoom("down",rooms.get((i-1)*5));
                }
                if (i+1<floor){

                    rooms.get(i*5).setNextRoom("up",rooms.get((i+1)*5));
                    rooms.get(i*5+4).setNextRoom("up",rooms.get((i+1)*5));

                }

            }



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bf!=null){
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


//        Room room1_1=new Room("1","1","新手村");
//        Room room1_2=new Room("1","2","黄土坡");
//        Room room1_3=new Room("1","3","杨柳坪");
//        Room room1_4=new Room("1","4","沙湾");
//        Room room1_5=new Room("1","5","团结村");
//
//
//        Room room2_1=new Room("2","1","第二层安全区");
//        Room room2_2=new Room("2","2","九龙村");
//        Room room2_3=new Room("2","3","丰田村");
//        Room room2_4=new Room("2","4","林格村");
//        Room room2_5=new Room("2","5","太平镇");

        //将房间和对应的进入方式放进集合

//        roomSet.getRoomHashMap().put("1",new ArrayList());
//        roomSet.getRoomHashMap().get("1").add(room1_1);
//        roomSet.getRoomHashMap().get("1").add(room1_2);
//        roomSet.getRoomHashMap().get("1").add(room1_3);
//        roomSet.getRoomHashMap().get("1").add(room1_4);
//        roomSet.getRoomHashMap().get("1").add(room1_5);
//
//
//        roomSet.getRoomHashMap().put("2",new ArrayList());
//        roomSet.getRoomHashMap().get("2").add(room2_1);
//        roomSet.getRoomHashMap().get("2").add(room2_2);
//        roomSet.getRoomHashMap().get("2").add(room2_3);
//        roomSet.getRoomHashMap().get("2").add(room2_4);
//        roomSet.getRoomHashMap().get("2").add(room2_5);




        //设置每个房间对应的出口方式和房间

//        room1_1.setNextRoom("up",room1_2);
//        room1_1.setNextRoom("4",room1_4);
//        room1_1.setNextRoom("3",room1_3);


//        room1_2.setNextRoom("down",room1_1);
//        room1_2.setNextRoom("3",room1_3);
//        room1_2.setNextRoom("5",room1_5);

//        room1_3.setNextRoom("2",room1_2);
//        room1_3.setNextRoom("4",room1_4);
//        room1_3.setNextRoom("1",room1_1);
//        room1_3.setNextRoom("5",room1_5);

//        room1_4.setNextRoom("3",room1_3);
//        room1_4.setNextRoom("1",room1_1);
//        room1_4.setNextRoom("5",room1_5);










    }
    public void initPskillSet(){

//        type 1  代表 群体技能  0 单题技能。
        Pskill pskill1 = new Pskill("s1","星爆弃疗斩",-50);
        pskill1.setBeishu(2.5);
        pskill1.setSkill_type(0);

        Pskill pskill2 = new Pskill("s2","尾兽玉",-50);
        pskill2.setBeishu(1.8);
        pskill2.setSkill_type(1);

        pskillSet.add(pskill1);
        pskillSet.add(pskill2);
    }
    public void initMskillSet(){

        BufferedReader bf = null;
        String t="s";
        ArrayList<Mskill> arrayList=new ArrayList<Mskill>();
        try {
            bf = new BufferedReader(new FileReader("./初始化/怪兽技能集合.txt"));
            String s=bf.readLine();
            while ((s=bf.readLine())!=null){
                String[] split = s.split("\t");
                if (!split[0].equals("")){

                    if(!t.equals(split[0])){
                        t=split[0];
                        arrayList.clear();
                    }
                    arrayList.add(new Mskill(split[0],split[1],split[2],Integer.parseInt(split[3]),Double.parseDouble(split[4])));
                    mskillSet.getMskills().put(split[0],(ArrayList<Mskill>) arrayList.clone());

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bf!=null){

                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

//        Mskill mskill1=new Mskill("ms1","龙皇异次元",0);
//        Mskill mskill2=new Mskill("ms2","青莲剑歌",0);
//
//
//        mskill1.setBeishu(3.6);
//        mskill2.setBeishu(4.5);
//
//        mskillSet.add(mskill1);
//        mskillSet.add(mskill2);
    }







    //传递指令
    public void play() {

        /**
         * @descript :整个游戏的处理逻辑
         * @author :ZhaoYicong
         * @date :2020/4/28 15:32
         * @Param:
         * @return :void
         * @throws :
         * @since :
         */


        ui.saywelocom();
        showPersonDate();
        showMonsterData();
        Scanner sc=new Scanner(System.in);
        int flag= 0;

        while (true){

            System.out.println("请输入你的指令：");


            String words = sc.nextLine();

            String[] word = words.split(" ");
            Handler handler=handlers.get(word[0]);
            String s="";
            if (word.length>1){
                s=word[1];
            }

            if (handler==null)
            {
                ui.warning();
            }else{
                if (handler.isBye()){
                    ui.bye();
                }
                if (handler!=null){

                    handler.doCommand(s);

                }

            }
            getFloorFlag();
            flag=gameOver();
            //flag 判断什么东西啊死了
            if (flag==1){
                System.out.println("恭喜通关！ 数据已保存");
                cundang();
                break;
            }
            if (flag==-1){
                System.out.println("抱歉你被杀死了,数据将会清空！");
                newgame();
                break;
            }
            monsterRevive();
            showPersonDate();
            showMonsterData();

        }



    }

    //
//    public Room rRoom(int min ,int max){
//        /**
//         * @descript : 生成随机数来选房子。
//         * @author :ZhaoYicong
//         * @date :2020/4/27 22:00
//         * @Param: min 房间的最小号数
//         * @Param: max  房间的最大号数
//         * @return :com.company.room.Room
//         * @throws :
//         * @since :
//         */
//
//
//
//        max=max+1;
//
//        int cha=max-min;
//        if(cha==1){
//            return this.roomSet.searchByID(min+"");
//        }else{
//            String num=((int)(Math.random()*cha+min))+"";
//            return this.roomSet.searchByID(num);
//
//        }
//
//
//
//    }

    //复杂指令的函数体现
    public void chop(String word){
        //淦的函数

//            Monster monster=null;
////
//        monster=personAttact(word);
//
//
//        if (monster==null){
//            System.out.println("没有这个怪兽。");
//        }else{
//            monsterAttact();
//
//        }
        personAttact(word);




    }
    public void goRoom(String word){
        /**
         * @descript :系统指令go
         * @author :ZhaoYicong
         * @date :2020/4/28 15:32
         * @Param: word
         * @return :void
         * @throws :
         * @since :
         */

        Room goroom =null;
        goroom = person.getNowRoom().searchNextRoomByID(word);
        if (goroom==null){
            System.out.println("去不了这间房间!");
        }else{
            this.person.setNowRoom(goroom);
        }
    }
    public int use(String[] words){
        /**
         * @descript :use 的逻辑处理
         * @author :ZhaoYicong
         * @date :2020/5/14 9:16
         * @Param: words
         * @return :int
         * @throws :
         * @since :
         */


        Pskill pskill=null;
        for (Pskill item :this.getPskillSet().getSkills()){
            if (item.getId().equals(words[0])){
                pskill=item;
                break;
            }
        }
        if (pskill==null){
            System.out.println("没有这个技能");
        }else{
            if (pskill.getSkill_type()==1){
                if (words[1].equals("all")){
                   for (Monster item:this.getMonsterSet().getMonsters() ){
                       if (item.getNowRoom().getFloor().equals(person.getNowRoom().getFloor())&& item.getNowRoom()==person.getNowRoom() && item.getHp()>0){
                           if (pskill.getTempol()==1){
                               person.setMp(person.getMp()-pskill.getMagicValue());
                           }
                           if (this.person.getMp()>=-pskill.getMagicValue()){
//                            this.person.setMp(this.person.getMp()+skill.getMagicValue());
                               pskill.useSkill(this.getPerson(),item);
                               System.out.println("你使用"+pskill.getName()+"对"+item.getName()+"造成了："+pskill.getHurtValu()+"点伤害");



                           }else{
                               System.out.println("没蓝了！");
                               break;
                           }
                       }
                   }

                    this.monsterAttact();
                    pskill.setTempol(0);
                }else{
                    System.out.println("这是群体技能，后面是all！");
                }

            }else{

                Monster monster = this.getMonsterSet().searchByIdandfloor(words[1],person);
                if (monster==null){
                    System.out.println("没有这个怪物");
                }else{
                    if(monster.getHp()<=0){
                        System.out.println("这个怪物已经死了！");
                    }else{


                        if (monster.getNowRoom()==this.person.getNowRoom()){
                            if (this.person.getMp()>=-pskill.getMagicValue()){
//                            this.person.setMp(this.person.getMp()+skill.getMagicValue());
                                pskill.useSkill(this.getPerson(),monster);
                                pskill.setTempol(0);
                                System.out.println("你使用"+pskill.getName()+"对"+monster.getName()+"造成了："+pskill.getHurtValu()+"点伤害");
                                this.monsterAttact();
//
                            }else{
                                System.out.println("没蓝了！");
                            }

                        }else{
                            System.out.println("无法攻击 怪物不在这个房间！");
                        }

                    }

                }
            }

        }
        return gameOver();

    }
    public int byShop(String word){
        int flag=0;
        Shop shop = this.shopSet.searchById(word);
        if (shop!=null){

            for (Shop item:shopSet.getShops()){
                if (item.getId().equals(word)){
                    flag=3;
                    if (person.getMoney()>=item.getSmoney()){


                        item.function(person);
                        monsterAttact();


                    }else{
                        System.out.println("抱歉你的金币不够了！");
                    }

                }
            }
        }
        return flag;
    }
    public void cundang(){
        BufferedWriter bw= null;
        try {
            bw = new BufferedWriter(new FileWriter("./初始化/人物初始化.txt"));
            String s1 ="id\thp\t钱\thp上限\tmp\t裸伤害\t通关卡\t武器id\t所在楼层\t房间序号\t铠甲id";
            String s2 =this.getPerson().getId()+"\t"+this.getPerson().getHp()+"\t"+this.getPerson().getMoney()+"\t"+
                    this.getPerson().getHplimit()+"\t"+this.getPerson().getMp()+"\t"+this.getPerson().getIniHurt()+"\t"+
                    this.getPerson().getFloor_flag()+"\t"+this.getPerson().getCurrentWeapon().getId()+"\t"+this.getPerson().getNowRoom().getFloor()+"\t"+
                    "1"+"\t"+this.getPerson().getArmor().getId()+"\t";
            bw.write(s1+"\n");
            bw.write(s2+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw!=null){

                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void newgame(){
        BufferedReader bf = null;
        BufferedWriter bw= null;
        try {
            bf = new BufferedReader(new FileReader("./初始化/人物初始化 - 副本.txt"));
            bw = new BufferedWriter(new FileWriter("./初始化/人物初始化.txt"));
            String s;
            while ((s=bf.readLine())!=null){
                bw.write(s+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bf!=null){

                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw!=null){

                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ui.bye();
    }




    //攻击函数
    public void monsterAttact(){
        //怪物攻击人的逻辑处理
        for (Monster item:monsterSet.getMonsters()){
            double random = Math.random();
            if (item.getType()==1 && random>=0.7&& item.getNowRoom()==this.person.getNowRoom()){
                int size = this.mskillSet.getMskills().get(item.getId()).size();
                int t=(int)(Math.random()*(size-0));

                Mskill mskill = this.mskillSet.getMskills().get(item.getId()).get(t);


                mskill.useSkill(item,person);
                System.out.println(item.getName()+"("+item.getId()+")"+"使用"+mskill.getName()+
                        "对你造成了："+mskill.getHurtValu()+"点伤害。");
            }
            else if (random>=0.3 && item.getHp()>0 && item.getNowRoom()==this.person.getNowRoom()){
                item.uesrArticle(item,item.getCurrentWeapon(),person);
                System.out.println(item.getName()+"("+item.getId()+")"+"使用"+item.getCurrentWeapon().getName()+
                        "对你造成了："+(item.getCurrentWeapon().getHurtValue()+item.getIniHurt())+"点伤害。");
            }
        }
    }
    public void personAttact (String word){
        //人物攻击怪物的逻辑  返回的monster 代表有没有这个怪物
        Monster monster=null;
        monster=monsterSet.searchByIdandfloor(word,person);
        if (monster==null){
            System.out.println("这一层没有这个怪兽");
        }else{
            if (monster.getNowRoom()==this.person.getNowRoom()){
                if(monster.getHp()<=0){
                    System.out.println("该怪物已经死了！");
                }else{
                    person.uesrArticle(person,person.getCurrentWeapon(), monster);
                    System.out.println("你使用"+person.getCurrentWeapon().getName()+"对"+monster.getName()+"造成了:"+(person.getCurrentWeapon().getHurtValue()+person.getIniHurt())+"点伤害");
                    monsterAttact();
                }

            }else{
                System.out.println("抱歉无法攻击他！！");
            }

        }

    }

    //一些判断函数

    public  void monsterRevive(){
        /**
         * @descript :判断怪物是否死亡并随机分配房间顺道给主角发工资。进行增强的处理 ！
         * @author :ZhaoYicong
         * @date :2020/4/28 15:26
         * @Param:
         * @return :void
         * @throws :
         * @since :
         */

        for (Monster item :monsterSet.getMonsters()){
            if (item.getHp()<=0 && item.getNowRoom()!=person.getNowRoom()){
                this.person.setMoney(this.person.getMoney()+item.getMoney());
                item.setHp(item.getHplimit());

            }
        }

    }
    public int gameOver(){
        /**
         * @descript : 判断人死还是boss死
         * @author :ZhaoYicong
         * @date :2020/5/14 10:28
         * @Param:
         * @return :int
         * @throws :
         * @since :
         */
        int flag=0;
        if (person.getHp()<=0){
            flag=-1;
        }

        if (monsterSet.searchById("boss").getHp()<=0){
            flag=1;
        }
        return flag;

    }
    public void getFloorFlag(){
        for (Monster item:monsterSet.getMonsters()){
            if (item.getType()==1 && person.getFloor_flag()<=Integer.parseInt(item.getNowRoom().getFloor())&&item.getHp()<=0){
                person.setFloor_flag(Integer.parseInt(item.getNowRoom().getFloor())+1);
                break;
            }
        }
    }

    //展示状态的函数
    public void showPersonDate(){
        System.out.println("****************************************************");
        System.out.println("*\t\t状态显示：");
        System.out.print("*你的名字是: "+person.getName());
        System.out.print("  血量是: "+person.getHp());
        System.out.print("  铠甲是: " + person.getArmor().getName());
        System.out.print("  魔法值："+person.getMp());
        System.out.print("  血量上限是: "+person.getHplimit());
        System.out.print("  武器"+"("+person.getCurrentWeapon().getName()+")"+"的攻击力是："+-person.getCurrentWeapon().getHurtValue());
        System.out.print("  \n*人物的攻击力是："+-person.getIniHurt());
        System.out.print("  攻击力点数是: "+-(person.getCurrentWeapon().getHurtValue()+person.getIniHurt()));
        System.out.println("  金币数量："+person.getMoney());
        System.out.println("*你当前在第"+person.getNowRoom().getFloor()+"层");
        System.out.println("*你当前的房间是: "+person.getNowRoom().getDescribe()+"("+person.getNowRoom().getId()+")");
        System.out.println("*你现在的出口有：");
//        for (Room item:person.getNowRoom().getNextroomhash().values()){
//            System.out.print("\uF06C"+item.getDescribe()+"("+item.getId()+")   ");
//        }
        Iterator iterator=person.getNowRoom().getNextroomhash().entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry= (Map.Entry) iterator.next();
            Object key =(String) entry.getKey();
            Room value= (Room) entry.getValue();
            System.out.print("\uF06C"+value.getDescribe()+"("+key+")   ");

        }
        System.out.println();
        System.out.println("****************************************************");
    }
    public void showMonsterData(){
        System.out.println();
        System.out.println("****************************************************");
        System.out.println("*\t\t这间房间怪物的状态如下:");

        for (Monster item:this.monsterSet.getMonsters()){
            if(item.getNowRoom().getFloor().equals(person.getNowRoom().getFloor())&&item.getNowRoom().getId().equals(person.getNowRoom().getId()) ){

                if (item.getHp()<=0){
                    System.out.print("*"+item.getName()+"("+item.getId()+")"+"死亡");

                }else{
                    System.out.print("*"+item.getName()+"("+item.getId()+")"+"的血量是："+item.getHp()+"\t他的攻击力是："+-(item.getCurrentWeapon().getHurtValue()+item.getIniHurt())+"\t他在："+item.getNowRoom().getDescribe()+"("+item.getNowRoom().getId()+")");
                    if (item.getType()==1){
                        System.out.println();
                        System.out.println("他的技能有:");

                        for (Mskill mskill:mskillSet.getMskills().get(item.getId())){
                            System.out.print("\t"+mskill.getName()+"   ");
                            mskill.skillDescription();
                        }
                    }

                }
                System.out.println();
            }
        }
        System.out.println("****************************************************");
    }
    public void showShop() {

        System.out.println("\t\t\t目前商店的物品有："+"(你的金币："+person.getMoney()+")");
            char c='@';
        for (Shop item:shopSet.getShops()){
//            if (item.getId().charAt(0)=='w'){
//                System.out.println("\n\t武器类：");
//            }else if (item.getId().charAt(0)=='s') {
//                System.out.println("\n\t药瓶类：");
//            }else if (item.getId().charAt(0)=='a'){
//                System.out.println("\n\t防御类：");
//            }
            if (item.getId().charAt(0)!=c){
                c=item.getId().charAt(0);
                if (c=='w'){
                System.out.println("\n\t武器类：");
            }else if (c=='s') {
                System.out.println("\n\t药瓶类：");
            }else if (c=='a'){
                System.out.println("\n\t防御类：");
            }
            }
                item.show();
        }
    }





    //以下是自动函数

    public Scene() {
    }

    public MskillSet getMskillSet() {
        return mskillSet;
    }

    public void setMskillSet(MskillSet mskillSet) {
        this.mskillSet = mskillSet;
    }



    public ShopSet getShopSet() {
        return shopSet;
    }

    public void setShopSet(ShopSet shopSet) {
        this.shopSet = shopSet;
    }

    public RoomSet getRoomSet() {
        return roomSet;
    }

    public void setRoomSet(RoomSet roomSet) {
        this.roomSet = roomSet;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public MonsterSet getMonsterSet() {
        return monsterSet;
    }

    public void setMonsterSet(MonsterSet monsterSet) {
        this.monsterSet = monsterSet;
    }

    public Ui getUi() {
        return ui;
    }

    public void setUi(Ui ui) {
        this.ui = ui;
    }

    public HashMap<String, Handler> getHandlers() {
        return handlers;
    }

    public void setHandlers(HashMap<String, Handler> handlers) {
        this.handlers = handlers;
    }
    public PskillSet getPskillSet() {
        return pskillSet;
    }

    public void setPskillSet(PskillSet pskillSet) {
        this.pskillSet = pskillSet;
    }
}
